package m.system.util;

import java.text.DateFormat;
import java.util.Date;
import java.util.Random;
/**
 * 调用方法,  获得20位长度的主键
 *  GenerateID.generatePrimaryKey()
 * @author admin
 *
 */
public class GenerateID {
	private final static char[] chars=new char[]{'0','1','2','3','4','5','6','7','8','9',
		'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
	private static final DateFormat format = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS");
	private static final long M=913543421221L;
	private static final int H=1;
	private static long lastTime= 0L;
	private static long index=36;
	private static long pi=36;
	private static long ln=36;
	private static long no=0;
	private static void init(){
		long time=new Date().getTime();
		if(time!=lastTime){
			int n=36+new Random().nextInt(36);
			lastTime=time;
			index=n;
			pi=n;
			ln=n;
			no=0;
		}
	}
	private static char getUnionKey(){
		return chars[H];
	}
	/**
	 * 获得主键 数据库唯一键
	 * 
	 */
	public synchronized static String generatePrimaryKey(){
		init();
		index=index+1;
		StringBuffer sb=new StringBuffer();
		toKey(sb,index);
		sb.insert(0,getUnionKey());
		toKey(sb,lastTime-M);
        return sb.insert(0,"I").toString();
	}
	private static final Random random=new Random();
	public static String upsetKey(String key) {
		if(StringUtil.isSpace(key)) return key;
		int len=key.length();
		int sp=random.nextInt(len-1)+1;
		sp= Math.min(sp, 35);
		return key.substring(sp) + chars[sp] + key.substring(0, sp);
	}
	/**
	 * 多服务器不唯一,数据库唯一主键不可使用
	 * 
	 */
	public synchronized static String tempKey(){
		init();
		pi=pi+1;
		StringBuffer sb=new StringBuffer();
		toKey(sb,pi);
		toKey(sb,lastTime-M);
	    return sb.insert(0,"P").toString();
	}
	/**
	 * 单号 20位
	 * 
	 */
	public synchronized static String generatePrimaryNo(){
		init();
		no=no+1;
		StringBuilder sb=new StringBuilder(format.format(new Date()));
		sb.append(getUnionKey());
		if(no<10){
			sb.append("0").append(no);
		}else if(no<100){
			sb.append(no);
		}else{
			try {
				Thread.sleep(1);
			} catch (InterruptedException ignored) {}
			return generatePrimaryNo();
		}
		return sb.toString();
	}
	public synchronized static Long generatePrimaryLong(){
		init();
		ln=ln+1;
		return (lastTime-M)*10000L+H*1000L +ln;
	}

	/**
	 * 把generatePrimaryKey或tempKey生成的字符串转换成时间
	 */
	public static Date parseDate(String key){
		if(key.startsWith("I")||key.startsWith("P")){
			char[] cs=key.substring(1,9).toCharArray();
			long t=M;
			for(int i=0;i<cs.length;i++){
				long n=toLong(cs.length-i-1,cs[i]);
				t+=n;
			}
			return new Date(t);
		}else{
			return null;
		}
	}
	private static long toLong(int n,char ch){
		for(int i=0;i<chars.length;i++){
			if(chars[i]==ch){
				long o=1;
				for(int m=0;m<n;m++){
					o=o*chars.length;
				}
				return i*o;
			}
		}
		return 0;
	}
	private static void toKey(StringBuffer sb,long time){
		//SystemUtil.println(chars[(int) (time%chars.length)]," -- ",time);
		sb.insert(0,chars[(int) (time%chars.length)]);
		if(time/chars.length>0){
			toKey(sb,time/chars.length);
		}
	}

}
